import { CdnConfig } from "../types/CdnConfig";
import axiosInstance from "./axios";

const API_URL = '/config/cdn';


export const addCdnConfig = async (cdnConfig: Omit<CdnConfig, 'id'>) => {
  try {
    const response = await axiosInstance.post(`${API_URL}/add`, cdnConfig);
    return response.data;
  } catch (error) {
    throw error;
  }
};

export const updateCdnConfig = async (cdnConfigId: number, cdnConfig: Partial<CdnConfig>) => {
  try {
    const updatedCdnConfig = { ...cdnConfig };
    if (updatedCdnConfig.domain) {
      updatedCdnConfig.domain = updatedCdnConfig.domain;
    }
    if (updatedCdnConfig.originIp) {
      updatedCdnConfig.originIp = updatedCdnConfig.originIp;
    }
    const response = await axiosInstance.put(`${API_URL}/update/${cdnConfigId}`, updatedCdnConfig);
    return response.data;
  } catch (error) {
    throw error;
  }
};

export const deleteCdnConfig = async (cdnConfigId: number) => {
  try {
    const response = await axiosInstance.delete(`${API_URL}/delete/${cdnConfigId}`);
    return response.data;
  } catch (error) {
    throw error;
  }
};

export const deployCdn = async (cdnConfigId: number) => {
  try {
    const response = await axiosInstance.put(`${API_URL}/deploy/${cdnConfigId}`);
    return response.data;
  } catch (error) {
    throw error;
  }
};

export const offlineCdn = async (cdnConfigId: number) => {
  try {
    const response = await axiosInstance.put(`${API_URL}/offline/${cdnConfigId}`);
    return response.data;
  } catch (error) {
    throw error;
  }
};

export const queryAllCdnConfigs = async () => {
  try {
    const response = await axiosInstance.get(`${API_URL}/query_all`);
    return response.data;
  } catch (error) {
    throw error;
  }
};

export const clearCache = async (cdnConfigId: number) => {
  try {
    const response = await axiosInstance.put(`${API_URL}/clear_cache/${cdnConfigId}`);
    return response.data;
  } catch (error) {
    throw error;
  }
};
